package guoanmaker.operator.business.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.modelnobind.AppraiseRo;

@Repository
public class OperatorAppraiseDao {
	@PersistenceContext
	EntityManager entityManager;

	public Map<String, Object> findAppraiseByParameters(int page, int pageSize,String orderNumber, String appraiseKind, String appraisePhone,
			String goodNumber, String shopName, String beginDate, String endDate) {
		String jpql = "select new guoanmaker.operator.business.modelnobind.AppraiseRo(p.id,p.orderForm.orderNumber,p.orderForm.commodity.commodityCode,p.orderForm.commodity.commodityName,"
				+ "p.orderForm.commodity.providerBusiness.businessName,p.orderForm.commodity.providerBusiness.LicensPhone,"
				+ "p.orderForm.commodity.serviceType.serverName,p.orderForm.user.username,p.appraiseTime,p.averageValue,p.appraise,p.isShow) from PersonalAppraise p where 1=1";
		
		if (orderNumber != null && !"".equals(orderNumber)) {
			jpql += " and p.orderForm.orderNumber like '%" + orderNumber + "%'";
		}
		if (appraiseKind != null && !"".equals(appraiseKind)) {
			if("差评".equals(appraiseKind)) {
				jpql += " and p.averageValue <= 2";
			} else if("好评".equals(appraiseKind)) {
				jpql += "and p.averageValue > 4";
			} else if("全部".equals(appraiseKind)){
				
			} else {
				jpql += "and p.averageValue > 2 and p.averageValue < 4";
			}
		}
		if (appraisePhone != null && !"".equals(appraisePhone)) {
			jpql += " and p.orderForm.user.username like '%" + appraisePhone + "%'";
		}
		if (goodNumber != null && !"".equals(goodNumber)) {
			jpql += " and p.orderForm.commodity.commodityCode like '%" + goodNumber + "%'";
		}
		if (shopName != null && !"".equals(shopName)) {
			jpql += " and p.orderForm.commodity.providerBusiness.businessName like '%" + shopName + "%'";
		}
		if ((beginDate != null && !"".equals(beginDate)) && (endDate != null && !"".equals(endDate))) {
		
			String dates = beginDate; 
			String date1 = endDate;
			dates = dates.replace("Z", " UTC");//注意是空格+UTC
			date1 = date1.replace("Z", " UTC");//注意是空格+UTC
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			Date date=null;
			Date date2 = null;
			String str="";
			String str1 = "";
			try {
				date = format.parse(dates);
				 str=sdf.format(date);  
				 date2 = format.parse(date1);
				 str1=sdf.format(date2); 
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			jpql += " and p.appraiseTime >='" + str + "' and p.appraiseTime <='" + str1 + "'";
		}
		jpql += " order by p.appraiseTime desc";
		Query query = entityManager.createQuery(jpql);
		Query query1 = entityManager.createQuery(jpql);
		Map<String, Object> map = new HashMap<>();
		map.put("totle", query1.getResultList().size());
		
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<AppraiseRo> appraiseRos = query.getResultList();
		map.put("result", appraiseRos);		
		appraiseRos.forEach(e -> {
			double result = e.getAppraiseAverage();
			if (result <= 2.0) {
				e.setAppraiseKind("差评");
			} else if (result > 4.0) {
				e.setAppraiseKind("好评");
			} else {
				e.setAppraiseKind("中评");
			}
		});
		return map;
	}
	
	
}
